<template>
  <div>
    <a-row :gutter="24" style="margin-bottom: 15px;margin-top: 20px;">
      <a-col :span="6">
        <div class="dashboard-card">
          <div class="card-header">
            <div class="card-title">空间资产总数</div>
            <div class="card-icon  card-blue">
              <a-icon type="shop" />
            </div>
          </div>
          <div class="card-value">{{ cardData.spaceAssetTotal }}</div>
          <div class="card-desc">空间资产数量</div>
        </div>
      </a-col>
      <a-col :span="6">
        <div class="dashboard-card">
          <div class="card-header">
            <div class="card-title">空间使用率</div>
            <div class="card-icon card-orange">
              <a-icon type="home" />
            </div>
          </div>
          <div class="card-value">{{ cardData.spaceUseRate }}%</div>
          <div class="card-desc">运营中空间占比</div>
        </div>
      </a-col>
      <a-col :span="6">
        <div class="dashboard-card">
          <div class="card-header">
            <div class="card-title">设备资产总数</div>
            <div class="card-icon card-green">
              <a-icon type="setting" />
            </div>
          </div>
          <div class="card-value">{{cardData.equipAssetTotal}}</div>
          <div class="card-desc">设备资产数量</div>
        </div>
      </a-col>
      <a-col :span="6">
        <div class="dashboard-card">
          <div class="card-header">
            <div class="card-title">设备完好率</div>
            <div class="card-icon card-purple">
              <a-icon type="bar-chart" />
            </div>
          </div>
          <div class="card-value">{{ cardData.equipGoodRate }}%</div>
          <div class="card-desc">正常运行设备占比</div>
        </div>
      </a-col>
    </a-row>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24" class="section-header">
            <a-col :span="16" class="section-title">
              <a-icon type="unordered-list" /> 资产清单
            </a-col>
            <a-col :span="8" style="text-align: right;">
              <a-button v-has="'assetMessage:add'" @click="handleAdd" type="primary" class="list-button" icon="plus">新增</a-button>
              <a-button v-has="'assetMessage:exportXls'" type="primary" icon="download" class="list-button" @click="handleExportXlsx('资产台账')">导出</a-button>
              <a-upload v-has="'assetMessage:importExcel'" name="file" :showUploadList="false" :multiple="false" accept=".xlsx" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel2">
                <a-button type="primary" icon="import">导入</a-button>
              </a-upload>
            </a-col>
          </a-row>
          <!-- 查询区域 -->
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="资产编码">
                <j-input placeholder="请输入资产编码" v-model="queryParam.assetCode"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="资产名称">
                <j-input placeholder="请输入资产名称" v-model="queryParam.assetName"></j-input>
              </a-form-item>
            </a-col>
            <template v-if="toggleSearchStatus">
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="资产类型">
                  <j-dict-select-tag placeholder="请选择资产类型" v-model="queryParam.type" dictCode="asset_type"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->
      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          class="j-table-force-nowrap"
          @change="handleTableChange">

          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text,record">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
            <a @click="handleDetail(record)">详情</a>
            <a-divider v-has="'assetMessage:edit'" type="vertical" />
            <a v-has="'assetMessage:edit'" @click="handleEdit(record)">编辑</a>
            <a-divider v-has="'assetMessage:isVisible'" type="vertical" />
            <a-popconfirm v-has="'assetMessage:isVisible'" v-if="record.isVisible != 1" :title="'确定将，资产'+record.assetName+'（'+record.assetCode+'）设为可见状态吗？'" @confirm="() => handleVisible(record.id, 1)">
              <a>可见</a>
            </a-popconfirm>
            <a-popconfirm  v-else :title="'确定将，资产'+record.assetName+'（'+record.assetCode+'）设为不可见状态吗？'" @confirm="() => handleVisible(record.id, 2)">
              <a>不可见</a>
            </a-popconfirm>
            <a-divider v-has="'assetMessage:delete'" type="vertical" />
            <a-popconfirm v-has="'assetMessage:delete'" :title="'确定删除，资产'+record.assetName+'（'+record.assetCode+'）吗？'" @confirm="() => handleDelete(record.id)">
              <a style="color: red">删除</a>
            </a-popconfirm>
          </span>
        </a-table>
      </div>
      <asset-message-modal ref="modalForm" @ok="modalFormOk"></asset-message-modal>
      <asset-message-view-modal ref="modalView" @ok="modalFormOk"  @edit="handleEdit"></asset-message-view-modal>
    </a-card>
  </div>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import AssetMessageModal from './modules/AssetMessageModal'
  import AssetMessageViewModal from './modules/AssetMessageViewModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'AssetMessageList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      AssetMessageModal,
      AssetMessageViewModal
    },
    data () {
      return {
        description: '资产台账管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'资产编码',
            align:"center",
            dataIndex: 'assetCode'
          },
          {
            title:'资产名称',
            align:"center",
            dataIndex: 'assetName'
          },
          {
            title:'资产类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'资产地点',
            align:"center",
            dataIndex: 'place'
          },
          {
            title:'资产状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'负责人',
            align:"center",
            dataIndex: 'chargePerson'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/asset/assetMessage/list",
          delete: "/asset/assetMessage/delete",
          deleteBatch: "/asset/assetMessage/deleteBatch",
          exportXlsUrl: "/asset/assetMessage/exportXls",
          importExcelUrl: "asset/assetMessage/importExcel",
          changeVisible: "/asset/assetMessage/changeVisible",
          cardTotal: "asset/assetMessage/cardTotal",
          
        },
        dictOptions:{},
        superFieldList:[],
        itemTitle: '资产详情',
        cardData:{
          spaceAssetTotal:0,
          spaceUseRate:0,
          equipAssetTotal:0,
          equipGoodRate:0,
        }
      }
    },
    created() {

    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      handleDetail:function(record){
        record.canEdit = true;
        this.$refs.modalView.edit(record);
        this.$refs.modalView.title="资产详情";
        this.$refs.modalView.disableSubmit = true;
      },
      handleVisible(id, visible){
        httpAction(this.url.changeVisible,{id, isVisible: visible},'post').then((res)=>{
          if(res.success){
            this.$message.success(res.message);
            this.loadData();
          }else{
            this.$message.warning(res.message);
          }
        }).finally(() => {

        })
      },
      loadDataEnd(){
        getAction(this.url.cardTotal, {}).then((res) => {
          if (res.success) {
            this.cardData = res.result;
          }
        });
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .dashboard-card {
    background: white;
    border-radius: 10px;
    padding: 15px 20px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
    transition: transform 0.3s, box-shadow 0.3s;
  }
  .dashboard-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  }
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }
  .card-title {
    font-size: 18px;
    color: #555;
    font-weight: 600;
  }
  .card-icon {
    width: 50px;
    height: 50px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: white;
  }
  .card-value {
    font-size: 24px;
    font-weight: bold;
    color: #333;
    margin-bottom: 5px;
  }
  .card-desc {
    color: #777;
    font-size: 0.9rem;
  }
  .card-blue {
    background: linear-gradient(45deg, #2196F3, #21CBF3);
  }
  .card-orange {
    background: linear-gradient(45deg, #FF9800, #FFC107);
  }
  .card-green {
    background: linear-gradient(45deg, #4CAF50, #8BC34A);
  }
  .card-purple {
    background: linear-gradient(45deg, #9C27B0, #E91E63);
  }
  .list-button {
      margin: 0 8px 8px 0;
  }
  .section-header{
    margin-bottom: 20px;
  }
  .section-title{
    font-size: 20px;
    color: #333;
    font-weight: 600;
  }
</style>